﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Hellquest
{
    public class CameraLockOn : Component
    {
        bool smooth = false;
        float smoothness = 0.3f;
        GameObject target;
        public CameraLockOn()
        {
            target = null;
        }

        public CameraLockOn(GameObject _target, bool _smooth)
        {
            target = _target;
            smooth = _smooth;
        }

        public override void Update(GameTime _gameTime)
        {
            if (target != null)
            {
                if (!smooth)
                    Position = target.transform.WorldPosition - Graphics.Instance.Resolution / 2;
                else
                {
                    Vector2 diff = target.transform.WorldPosition - Graphics.Instance.Resolution / 2 - Position;
                    if (diff.LengthSquared() > 3)
                        Position += diff / (smoothness / (float)_gameTime.ElapsedGameTime.TotalSeconds);
                    else
                        Position = target.transform.WorldPosition - Graphics.Instance.Resolution / 2;
                }
                    
            }
        }
    }
}
